import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  JoinColumn,
  BeforeInsert,
  BeforeUpdate,
} from 'typeorm';
import { MatchInfo } from 'src/modules/match/entities/match.entity';
import { GameStatusEnum } from 'src/common/constant/enums';
import { EventToPlayer } from 'src/modules/event-to-player/entities/event-to-player.entity';
import { RallyRule } from 'src/modules/rally-rule/entities/rally-rule.entity';
import { BaseEntity } from 'src/common/entity/BaseEntity';

@Entity('game_info')
export class GameInfo extends BaseEntity {
  @Column({ name: 'homePlayer' })
  homePlayerId: number; // 主场选手编码

  @Column({ name: 'awayPlayer' })
  awayPlayerId: number; // 客场选手编码

  @Column({ name: 'winner' })
  winnerId: number; // 胜利者编码

  @ManyToOne(() => EventToPlayer)
  @JoinColumn({ name: 'homePlayer' })
  homePlayer: EventToPlayer;

  @ManyToOne(() => EventToPlayer)
  @JoinColumn({ name: 'awayPlayer' })
  awayPlayer: EventToPlayer;

  @ManyToOne(() => EventToPlayer)
  @JoinColumn({ name: 'winner' })
  winner: EventToPlayer;

  @Column('json', { nullable: true })
  progress: any;

  @Column({ type: 'datetime', nullable: true })
  startTime: Date;

  @Column({ type: 'datetime', nullable: true })
  endTime: Date;

  @Column()
  status: GameStatusEnum;

  @Column()
  gameIndex: number;

  @Column({ name: 'fk_match_id' })
  matchId: number;

  @ManyToOne(() => MatchInfo, (matchInfo) => matchInfo.games)
  @JoinColumn({ name: 'fk_match_id' })
  matchInfo: MatchInfo;

  @Column()
  homePlayerRally: number;
  @Column()
  awayPlayerRally: number;

  @ManyToOne(() => RallyRule)
  @JoinColumn({ name: 'fk_rallyrule_id' })
  rallyRule: RallyRule;

  @Column({ name: 'fk_rallyrule_id' })
  rallyRuleId: number;
}
